package co.fitstart.admin.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import co.fitstart.admin.dao.ContentDao;
import co.fitstart.admin.service.NewsService;
import co.fitstart.entity.content.News;

import com.demo2do.core.persistence.annotation.Dynamic;
/**
 * 
 * @author lidan
 *
 */
@Service("newsService")
public class NewsServiceImpl extends DataServiceImpl<News> implements NewsService {
    
    @Dynamic
    private ContentDao contentDao;

    /* (non-Javadoc)
     * @see co.fitstart.admin.service.content.NewsService#stick(co.fitstart.common.entity.content.News, int)
     */
    public void stick(News news, int maxStickyLimit) {
        
        List<News> sticks = contentDao.listStickNews();

        if(sticks.isEmpty()) { // no sticky ones
            
            genericDaoSupport.update(news.stick(100));
            
        } else {
            
            // stick the activity using the 
            News last = sticks.get(sticks.size() - 1);
            genericDaoSupport.update(news.stick(last.getSerial()));
            
        }
        
        if(sticks.size() == maxStickyLimit) { // unstick the first one when the sticky queue is full
            
            News first = sticks.get(0);
            genericDaoSupport.update(first.unstick());
            
        } 
        
    }

    /* (non-Javadoc)
     * @see co.fitstart.admin.service.content.NewsService#unstick(co.fitstart.common.entity.content.News)
     */
    public void unstick(News news) {
        genericDaoSupport.update(news.unstick());
    }


}
